package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.buildtable.pojo.Role;
import com.example.buildtable.pojo.Role;
import com.example.buildtable.pojo.Role;
import com.example.common.util.R;
import com.example.buildtable.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "角色")
@RequestMapping(value = "/role/")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Operation(summary = "查询所有角色数据")
    @GetMapping(value = "/getAllRole")
    public R getAllRole() {
        List<Role> roleList = roleService.list();
        return R.ok("查询所有角色数据成功").data("roleList", roleList);
    }

    @Operation(summary = "查询所有角色分页数据")
    @GetMapping(value = "/getRolePage/{page}/{limit}")
    public R getRolePage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Page<Role> userRolePage = new Page<>(page, limit);
        roleService.page(userRolePage, null);
        //每页的数据集合
        List<Role> userRoleList = userRolePage.getRecords();
        //总的记录数
        Long total = userRolePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", userRoleList);
        return R.ok("查询所有管理员角色分页数据成功").data("map", map);
    }

    @Operation(summary = "增加角色")
    @PostMapping(value = "/addRole")
    public R addRole(@RequestBody Role role) {
        boolean flag = roleService.save(role);
        if (flag) {
            return R.ok("增加角色成功");
        } else {
            return R.ok("增加角色失败");
        }
    }

    @Operation(summary = "删除角色")
    @GetMapping(value = "/deleteRole/{roleId}")
    public R deleteRole(@PathVariable("roleId") Integer roleId) {
        boolean flag = roleService.removeById(roleId);
        if (flag) {
            return R.ok("删除角色成功");
        } else {
            return R.ok("删除角色失败");
        }
    }

    @Operation(summary = "按照角色编号查看")
    @GetMapping(value = "/getRoleById/{roleId}")
    public R getRoleById(@PathVariable("roleId") Integer roleId) {
        Role role = roleService.getById(roleId);
        if (role != null) {
            return R.ok("按照角色编号查看成功").data("role", role);
        } else {
            return R.error("按照角色编号查看失败");
        }
    }

    @Operation(summary = "修改角色")
    @PostMapping(value = "/updateRole")
    public R updateRole(Role role) {
        boolean flag = roleService.updateById(role);
        if (flag) {
            return R.ok("修改角色成功");
        } else {
            return R.ok("修改角色失败");
        }
    }

    @Operation(summary = "修改角色的状态")
    @PostMapping(value = "/updateRoleStatus")
    public R updateRoleStatus(@RequestBody Role role) {
        Integer status = role.getStatus();
        if (status == 1 || status == 0) {
            int newStatus = (status == 1) ? 0 : 1;
            role.setStatus(newStatus);
            boolean updated = roleService.updateById(role);

            String statusText = null;
            if (updated) {
                statusText = (newStatus == 1) ? "可用" : "不可用";
                return R.ok(statusText + role.getRoleName() + "成功").data("role", role);
            } else {
                return R.error(statusText + role.getRoleName() + "失败");
            }
        } else {
            return R.error("无效的状态值");
        }
    }
}
